package com.zh.test.longListener;







import org.springframework.util.StreamUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author zh
 * @Description  使用while关键词一直读取本地文件信息
 * @Date 2020/11/4
 * @Param
 * @return
 **/
public class listener {
    public static void main(String[] args) {
        TimeUnit unit;
        BlockingQueue workQueue;
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 10, 1000 ,TimeUnit.MILLISECONDS, new ArrayBlockingQueue(10));
        threadPoolExecutor.execute(()->{
            while(true){
                FileReader fileReader = null;
                File file = null;
                try {
                 //   file =  new File();
//                    fileReader = new FileReader("D:\\test2.log");
//                   char[] chars = new char[10];
//                    int read = fileReader.read(chars);
//                    System.out.println(new String(chars,0,read));
//                    String s = new String(chars);
//                    byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
//                    System.out.println(new String(bytes));
                    file = new File("D:\\test2.log");

                    InputStream inputStream = new FileInputStream(file);
                    byte[] b = new  byte[1024];
                    int read = inputStream.read(b);
                    String str = new String(b,0,read);
                    byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
                    String str2 = new String(bytes);
                    System.out.println(str2);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fileReader!=null){
                        try {
                            System.out.println("关闭流");
                            fileReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        });
    }

}
